package com.zx.mes.senior.juc;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 使用多线程来改变值,发现问题所在(一个线程对应一个独立的内存空间)
 *
 * @author 华云龙
 * @date 2018-11-26
 */
public class UpdateFalg {

    public static void main(String []args){
        TestThread td = new TestThread();
        Thread thread = new Thread(td);
        thread.start();
        while (true) {
            System.out.println(td.getFlag());
            if (td.getFlag()) {
                System.out.println("停止 flag:"+td.getFlag());
                break;
            }
        }
    }
}

@Data
@Accessors(chain = true)
class TestThread implements Runnable{

    private Boolean flag = false;

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        setFlag(true);
        System.out.println("flag:"+flag);
    }
}
